<?php
/**
 * Desc: 自定义的log文件驱动，改个格式适应ELK日志系统
 * Created by xiejianwen@me.com
 * Date: 2016/10/18 18:08
 */

namespace log;


class LocalLogFileDriver {

    protected $config = [
        'time_format' => ' c ',
        'file_size'   => 2097152,
        'path'        => LOG_PATH,
        'apart_level' => [],
    ];

    // 实例化并传入参数
    public function __construct($config = []) {
        if(is_array($config)) {
            $this->config = array_merge($this->config, $config);
        }
    }

    /**
     * 日志写入接口
     * @access public
     * @param array $log 日志信息
     * @return bool
     */
    public function save(array $log = []) {
        if($log && isset($log['elk'])) {
            $elkLog = $log['elk'];

            $filename = ELK_LOG_PATH . date('Y-m-d') .'-console' . '.log';
            $path     = dirname($filename);
            !is_dir($path) && mkdir($path, 0755, true);
            error_log(json_encode($elkLog) . "\r\n", 3, $filename);
        }

        return true;
    }
}